package competences;

import data.ECaracteristique;

public class ListeCompetences {
	
	private Competence liste[];
	private int caracteristiques[];
	
	public ListeCompetences (int caracteristiques[]) {
		liste = new Competence[ECompetence.values().length];
		this.caracteristiques = caracteristiques;
	}
	
	public int getValeur (ECompetence c) {
		Competence comp = liste[c.ordinal()];
		if (comp != null) {
			int modTrait = calculModTrait (comp.getTraitCle());
			return comp.getValeur(modTrait);
		}
		else
			return 0;
	}
	
	public boolean addRang (ECompetence c, int niveau) {
		Competence comp = liste[c.ordinal()];
		if (comp != null)
			if (comp.getRang() < niveau+3) {
				comp.addRang();
				if ((comp.getRang() == 5) && (comp.getSynergie() != null)) {//Pour la synergie
					addBonus (comp.getSynergie());
					addBonus (comp.getSynergie());
				}
				return true;
			}
			else
				return false;
		else {
			createCompetence(c);
			liste[c.ordinal()].addRang();
			return true;
		}
		
	}
	
	public void addBonus (ECompetence c) {
		if (liste[c.ordinal()] == null)
			createCompetence(c);
		liste[c.ordinal()].addBonus();
	}
	
	private void createCompetence (ECompetence c) {
		try {
			liste[c.ordinal()] = (Competence) Class.forName("competences."+c).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			liste[c.ordinal()] = new Artisanat ();
			System.out.println(c + " not found");
		}
	}
	
	private int calculModTrait (ECaracteristique carac) {
		int modTrait = caracteristiques[carac.ordinal()]-10;
		if (modTrait < 0)
			modTrait--;
		modTrait /= 2;
		
		return modTrait;
	}

	@Override
	public String toString() {
		String retour = "Liste de competences :\n";
		for (ECompetence c : ECompetence.values()) {
			Competence comp = liste[c.ordinal()];
			if (comp != null) {
				int modTrait = calculModTrait (comp.getTraitCle());
				retour += comp.toString(modTrait) + "\n"; //c.toString() + " : " + comp.getValeur(modTrait) + "\n";
			}
		}
		
		return retour;
	}
	
	
}
